package com.treasure.common.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * @author sunhongmin
 * @date 2023/9/1 17:27
 * @description
 */
@UtilityClass
public class TSON {
    private static Gson GSON = new GsonBuilder()
            .create();

    public static <T> String toJsonString(T object) {
        if (Objects.isNull(object)) {
            return null;
        }
        return GSON.toJson(object);
    }

    public static <T> T parseObject(String jsonString, Class<T> clazz) {
        if (StringUtils.isBlank(jsonString)) {
            return null;
        }
        return GSON.fromJson(jsonString, clazz);
    }

    public static <T> T parseObject(String jsonString, TypeToken<T> typeToken) {
        if (StringUtils.isBlank(jsonString)) {
            return null;
        }
        return GSON.fromJson(jsonString, typeToken.getType());
    }
}
